import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import jdk.jfr.DataAmount;

public class CodeGenerator {


    public static void main(String[] args) {

//        String url = "jdbc:mysql://127.0.0.1:3306/spider?useSSL=false&useUnicode=true&characterEncoding=UTF-8";
//        String username = "root";
//        String password = "root";
//        gen(url, username, password);

//        gbData();
//        ntspDev();
        dataSyncTemp();
//        userCenterDev();
    }
    private static void dataSyncTemp() {
        String basePackage = "com.chery.lion.data.sync";
        String url = "jdbc:mysql://172.25.171.7:3306/ntsp_stsp_temp?useUnicode=true&characterEncoding=utf-8&rewriteBatchedStatements=true&useSSL=false";
        String username = "ntsp_stsp_temp_rw";
        String password = "4xhuRpwZP7wldJCG";
        String[] tableNames = "ntsp_vehicle_brand, ntsp_vehicle_series, ntsp_inside_vehicle_type, ntsp_vehicle_info, ntsp_outside_dtc_config".split(",");
        gen(new GenParam(url, username, password, basePackage, tableNames));
    }

    private static void gbData() {
        String basePackage = "com.chery.lion.gb.dgs.task";
        String url = "jdbc:mysql://172.25.171.7:3306/lioncloud_landmarkgb?useUnicode=true&characterEncoding=utf-8&rewriteBatchedStatements=true";
        String username = "lioncloud_landmarkgb_rw";
        String password = "1bTAxVWTlsWVBE7a";
        String tableNames = "gb_total_task";
        gen(new GenParam(url, username, password, basePackage, tableNames.split(",")));
    }

    private static void ntspDev() {
        String basePackage = "com.chery.lion.data.sync";
        String url = "jdbc:mysql://172.25.171.7:3306/chery_bfg_ntsp?useUnicode=true&characterEncoding=utf-8&rewriteBatchedStatements=true&allowMultiQueries=true";
        String username = "chery_bfg_ntsp_rw";
        String password = "+R-2+aoe7ccYF3M+";
        String tableNames = "ntsp_material_info";
        gen(new GenParam(url, username, password, basePackage, tableNames.split(",")));
    }

    private static void userCenterDev() {
        String basePackage = "com.chery.account.sync";
        String url = "jdbc:mysql://172.25.171.7:3306/bfg_user_center?useUnicode=true&characterEncoding=utf-8&rewriteBatchedStatements=true&allowMultiQueries=true";
        String username = "chery_bfg_ntsp_rw";
        String password = "+R-2+aoe7ccYF3M+";
        String tableNames = "uc_b_user_info,uc_b_franchiser_info,uc_b_user_role";
        gen(new GenParam(url, username, password, basePackage, tableNames.split(",")));
    }

    public static void gen(GenParam param) {
        String url = param.getUrl();
        String username = param.getUsername();
        String password = param.getPassword();
        String basePackage = param.getBasePackage();
        String[] tableNames = param.getTableNames();
        if (tableNames != null && tableNames.length > 0) {
            for (int i = 0; i < tableNames.length; i++) {
                String tableName = tableNames[i].trim();
                tableNames[i] = tableName;
            }
        }
        FastAutoGenerator.create(url, username, password)
                .globalConfig(builder -> builder
                        .author("wugaoyang")
                        .outputDir("src/main/gen")
                        .disableOpenDir()
                        .commentDate("yyyy-MM-dd")
                )
                .packageConfig(builder -> builder
                        .parent(basePackage)
                        .entity("entity")
                        .mapper("dao")
                        .service("service")
                        .serviceImpl("service.impl")
                        .xml("mapper.xml")
                )
                .strategyConfig(builder -> builder
                                .addInclude(tableNames)
                                .entityBuilder()
                                .enableLombok()
                                .disableSerialVersionUID()
                                .enableFileOverride()

                                .mapperBuilder()
                                .enableFileOverride()
//                        .enableBaseResultMap()
//                        .enableBaseColumnList()
                )
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();
    }
}